package com.hulk.ratel.service;

import cn.ms.sequence.IdWorker;
import com.google.common.base.Strings;
import com.hulk.common.util.DateUtil;
import com.hulk.ratel.common.constant.Constant;
import com.hulk.ratel.common.constant.enums.TxnLogStatus;
import com.hulk.ratel.common.constant.enums.TxnProcessStep;
import com.hulk.ratel.component.R;
import com.hulk.ratel.manage.bo.InitiativeScanPayReqBO;
import com.hulk.ratel.manage.org.InitiativeScanPayManager;
import com.hulk.ratel.persistence.entity.TLogTxnPayRecord;
import com.hulk.ratel.persistence.entity.TPartnerInfo;
import com.hulk.ratel.persistence.entity.TTxncode;
import com.hulk.ratel.service.dto.InitiativeScanPayReqDTO;
import com.hulk.ratel.service.dto.InitiativeScanPayRespDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @Author: hulk
 * @E-mail: 29572320@qq.com
 * @Date: Create in 2018/8/31 下午10:14
 * @Version: 1.0.0
 * @Modify by:
 * Class description
 */
@Slf4j
@Service(value = "orderPayService")
public class InitiativeScanPayServiceImpl extends AbsServiceImpl implements InitiativeScanPayService {

    @Autowired
    private InitiativeScanPayManager  initiativeScanPayManager;

    @Override
    public R<InitiativeScanPayRespDTO> excute(InitiativeScanPayReqDTO dto, String intCutDate, String workId) {

        intCutDate = Optional.ofNullable(intCutDate).orElse(DateUtil.getDateyyyyMMdd());
        workId = Optional.ofNullable(workId).orElse(String.valueOf(IdWorker.getId()));
        validateBean(dto, workId);


        String txnCode = dto.getHead().getTxnCode();
        String partnerNo = dto.getHead().getPartnerNo();
        String traceId = dto.getHead().getTraceId();

        //合作方信息校验
        TPartnerInfo partnerInfo = checkPartner(partnerNo, workId);
        //交易权限校验
        TTxncode txnCodeInfo = checkTxnCode(txnCode, workId);
        //交易重复性校验
        checkTxnLog(traceId, workId, partnerNo);


        //商户信校验
        //TbPoolRealMerchantInfo realMerchant = super.checkMerchantInfo(partnerInfo.getPartnerNo(), reqDto.getMerchantCode(), platformId);

        //真实商户生成支付流水
        TLogTxnPayRecord record = this.initTxnLog(partnerInfo, txnCodeInfo, dto.getMerchantCode(), dto, intCutDate, workId);
        //业务处理
       // payApplyResp = this.dealRealMerchantTxn(reqDto, record, platformId, traceId, realMerchant.getSettleType());

        //
        InitiativeScanPayReqBO reqBO  = new InitiativeScanPayReqBO();

        initiativeScanPayManager.doPay(reqBO,record,workId);

        return null;
    }

    /**
     * 支付流水初始化
     */
    private TLogTxnPayRecord initTxnLog(
            TPartnerInfo partnerInfo,
            TTxncode txnCode,
            String merchantCode,
            InitiativeScanPayReqDTO reqDto,
            String intCutDate,
            String workId
    ) {
        TLogTxnPayRecord record = new TLogTxnPayRecord();
        record.setId(IdWorker.getId());//主键生成
        record.setLockVersion(Constant.ZERO_LONG);
        record.setIntCutDate(intCutDate);
        record.setWorkId(workId);
        record.setCallBackUrl(reqDto.getCallBackUrl());
        record.setVersion(reqDto.getHead().getVersion());
        record.setPartnerNo(partnerInfo.getPartnerNo());
        record.setMerchantCode(merchantCode);//商户号
        record.setOverorgCode(reqDto.getOverOrgCode());//末端支付机构号
        record.setPartnerType(partnerInfo.getPartnerType());//合作方类型
        record.setTraceId(reqDto.getHead().getTraceId());
        record.setTxnType(txnCode.getTxnType());
        record.setTxnCode(txnCode.getTxnCode());
        record.setReqDate(reqDto.getHead().getReqDate());
        record.setReqTime(reqDto.getHead().getReqTime());
        if (!Strings.isNullOrEmpty(reqDto.getExpireTime())) {
            record.setExpireTime(Integer.valueOf(reqDto.getExpireTime()));
        } else {
            record.setExpireTime(Constant.ONE_DAY_MINUTES);
        }
        record.setTotalAmt(Integer.valueOf(reqDto.getTotalAmt()));
        record.setSubject(reqDto.getSubject());
        record.setDescription(reqDto.getDesc());
        record.setGoodsTag(reqDto.getGoodsTag());
        record.setBuyerPayAmt(null);
        record.setFavorAmt(null);
        record.setStep(TxnProcessStep.ACCEPTED.getValue());
        record.setStatus(TxnLogStatus.CREATED.getValue());
        record.setTerminalNo(reqDto.getTerminalNo());//用户号码
        record.setClientIp(reqDto.getTerminalIp());
        logTxnPayRecordRepository.insert(record);
        return record;
    }



}
